package com.opengroupe.activity;


import java.util.List;


import org.json.JSONException;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckedTextView;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

import com.opengroupe.R;
import com.opengroupe.dto.CandidatDto;
import com.opengroupe.dto.QuestionnaireDto;


public class AssignerQuestionnaireActivity extends Activity {
	
	private Spinner qSpinner;
	private Button btnAssigner;
	private TextView txtAssignError;
	
	//private WebService webService;
	
	private String tag;
	
	
	//private Gson gson = new GsonBuilder().serializeNulls().setPrettyPrinting().create();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        
		
        setContentView(R.layout.activity_assigner_questionnaire);
        
        //webService = new WebService(getString(R.string.ws_url));
        
        tag = getString(R.string.log_prefix);
        
        qSpinner = (Spinner) findViewById(R.id.qSpinner);
        btnAssigner = (Button) findViewById(R.id.qButton);
        txtAssignError = (TextView) findViewById(R.id.assignError);
        
        try {
			initializeSpinner();
		} catch (JSONException e) {
			Log.d(getString(R.string.log_prefix), e.getMessage());
		}
        
        btnAssigner.setOnClickListener(btnAssignerListener);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_assigner_questionnaire, menu);
        return true;
    }

    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        return super.onOptionsItemSelected(item);
    }
	
	private void initializeSpinner() throws JSONException {
		//String response = webService.webGet("categorie/getAll", new HashMap<String, String>(0));
		//Log.d(tag + "reponse GET categorie/getAll = ", response);
		
		
		List<QuestionnaireDto> questionnaires = QuestionnaireDto.getAll(getApplicationContext());
		
		//ArrayAdapter<QuestionnaireDto> adapter = new ArrayAdapter<QuestionnaireDto>(this, R.layout.spinner, array);
        //adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        qSpinner.setAdapter(new ArrayAdapter<QuestionnaireDto>(this, android.R.layout.simple_spinner_item, questionnaires) {
        	public View getView(int position, View convertView, ViewGroup parent) {
        		View v = convertView;
        		
        		if (v == null)
        		{
        			LayoutInflater inflater = ((Activity) this.getContext()).getLayoutInflater();
                    v = inflater.inflate(android.R.layout.simple_spinner_item, parent, false);
        		}
        		
        		
        		TextView  tbQuestionnaireLibelle = (TextView) v.findViewById(android.R.id.text1);
        		if (tbQuestionnaireLibelle.getTag() == null)
        		{
        			QuestionnaireDto questionnaire = this.getItem(position);
        			
        			tbQuestionnaireLibelle.setText(questionnaire.getLibelle());
        			tbQuestionnaireLibelle.setTag(questionnaire);
        		}
        		
        		return v;
        	}
        });
	}

    
    private OnClickListener btnAssignerListener = new OnClickListener() {
		
		public void onClick(View v) {
			Bundle bundle = getIntent().getExtras();
			
			CandidatDto candidat = (CandidatDto) bundle.get("candidat");
			Log.d(tag + "emailCandidat = ", candidat.getEmailCandidat());
			
			QuestionnaireDto questionnaire = (QuestionnaireDto) qSpinner.getSelectedItem();
			
			Log.d(tag + "idQuestionnaire =", String.valueOf( questionnaire.getIdQuestionnaire()) );
			
			candidat.updateQuestionnaire(getApplicationContext(), questionnaire.getIdQuestionnaire());
			
			
			Intent intent = new Intent(getApplicationContext(),MenuActivity.class);
			startActivityForResult(intent, 0);
			
			
		}
	};
}
